<template>
  <div>
    <!-- 搜索结果页-头部导航 -->
    <div class="search-result-container">
      <!-- 点击实现后退效果 -->
      <van-nav-bar
        title="搜索结果"
        left-arrow
        @click-left="$router.go(-1)"
        fixed
      />
    </div>
    <!-- 搜索结果 -->
    <div>
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        :immediate-check="false"
      >
        <ArticleItem
          v-for="artObj in articleList"
          :key="artObj.art_id"
          :artObj="artObj"
          :isShow="false"
        />
      </van-list>
    </div>
  </div>
</template>

<script>
import ArticleItem from '@/components/articleItem.vue'
import { getSearchAPI } from '@/api'
export default {
  name: 'SearchResult',
  data () {
    return {
      page: 1, // 页码
      articleList: [], // 文章数据
      loading: false, // 加载状态
      finished: false // 是否全部加载完成
    }
  },
  created () {
    this.getSearchFn()
  },
  components: {
    ArticleItem
  },
  methods: {
    // 列表触底方法
    onLoad () {
      if (this.articleList.length > 0) {
        this.loading = false
        this.page++
        this.getSearchFn()
      }
    },
    // 获取文章信息
    async getSearchFn () {
      const { data: res } = await getSearchAPI({
        page: this.page,
        q: this.$route.params.kw
      })

      if (!res.data.results.length) return (this.finished = true)

      this.articleList = [...this.articleList, ...res.data.results]
    }
  }
}
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
</style>
